package god530.com.simpleapp_slide.account.title.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import java.util.ArrayList;
import java.util.List;

import god530.com.simpleapp_slide.account.tabs.account.model.AccountModel;

/**
 * Created by root on 16-1-31.
 */
public class AccountTypeDBHelper extends DBBase {
    private static AccountTypeDBHelper accountTypeDBHelper;

    public AccountTypeDBHelper(Context context) {
        super(context);
    }

    public static AccountTypeDBHelper getInstance(Context context) {
        if(accountTypeDBHelper == null) {
            accountTypeDBHelper = new AccountTypeDBHelper(context);
        }
        return accountTypeDBHelper;
    }

    public void insert(/*String table, */AccountModel accountModel) {
        initDataBase();
        ContentValues values = new ContentValues();
//        values.put(DBConstants.ACCOUNTTYPE_ID, accountModel.getId());
        values.put(DBConstants.ACCOUNTTYPE_KEY, accountModel.getKey());
        values.put(DBConstants.ACCOUNTTYPE_VALUE, accountModel.getValue());
        values.put(DBConstants.ACCOUNTTYPE_MONEY, accountModel.getMoney());
        db.insert(DBConstants.TABLE_ACCOUNTTYPE, null, values);
    }

    public void delete(AccountModel accountModel/*String table, String whereClause, String[] whereArgs*/) {
        initDataBase();
        String whereClause = DBConstants.ACCOUNTTYPE_ID + "=? and " + DBConstants.ACCOUNTTYPE_VALUE + "=?";
        String[] whereArgs = new String[]{String.valueOf(accountModel.getId()), accountModel.getValue()};
        db.delete(DBConstants.TABLE_ACCOUNTTYPE, whereClause, whereArgs);
    }

    public void update(AccountModel accountModel/*String table, ContentValues values, String whereClause, String[] whereArgs*/){
        initDataBase();
        ContentValues values = new ContentValues();
        values.put(DBConstants.ACCOUNTTYPE_MONEY, accountModel.getMoney());
        values.put(DBConstants.ACCOUNTTYPE_KEY, accountModel.getKey());
        values.put(DBConstants.ACCOUNTTYPE_VALUE, accountModel.getValue());
        String whereClause = DBConstants.ACCOUNTTYPE_ID+"=?";
        String[] whereArgs = new String[]{ String.valueOf(accountModel.getId()) };
        db.update(DBConstants.TABLE_ACCOUNTTYPE,values,whereClause,whereArgs);
    }

    public List<AccountModel> query(/*String table, String[] columns, String selection, String[] selectionArgs
            , String groupBy, String having, String orderBy, String limit*/){
        initDataBase();
        Cursor cursor = db.query(DBConstants.TABLE_ACCOUNTTYPE,
                null
                /*new String[]{
                    DBConstants.ACCOUNTTYPE_ID ,
                    DBConstants.ACCOUNTTYPE_KEY ,
                    DBConstants.ACCOUNTTYPE_MONEY ,
                    DBConstants.ACCOUNTTYPE_MONEY
                }*/
                ,null, null, null, null, null, null);
        cursor.moveToFirst();
        List<AccountModel> listAccountModel = new ArrayList<>();
        while(cursor.moveToNext()) {
            int _id = cursor.getColumnIndex(DBConstants.ACCOUNTTYPE_ID);
            int _key = cursor.getColumnIndex(DBConstants.ACCOUNTTYPE_KEY);
            int _value = cursor.getColumnIndex(DBConstants.ACCOUNTTYPE_VALUE);
            int _money = cursor.getColumnIndex(DBConstants.ACCOUNTTYPE_MONEY);
            int id = cursor.getInt(_id);
            String key = cursor.getString(_key);
            String value = cursor.getString(_value);
            double money = cursor.getDouble(_money);

            AccountModel accountModel = new AccountModel(id, key, value, money);
            listAccountModel.add(accountModel);
        }
        return listAccountModel;
    }



}
